﻿using System;
using System.Windows.Data;

namespace AnthStat.WPF.Applications.Converters
{
    public sealed class ChartConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // TODO: Code contracts

            //if (values.Length != 4)
            //{
            //    return Double.NaN;
            //}
            //else if (values[0] == null)
            //{
            //    return Double.NaN;
            //}

            var target = values[0];
            object objValue = target.GetType().GetProperty(values[1].ToString()).GetValue(target);

            if (objValue == null)
            {
                return Double.NaN;
            }

            double rawValue = (double)objValue;

            //double rawValue = (double)values[1];
            double actualSize = (double)values[2];
            double axisStartValue = (double)values[3];
            double axisEndValue = (double)values[4];

            double ratio = actualSize / (axisEndValue - axisStartValue);

            rawValue = rawValue - axisStartValue;

            return rawValue * ratio;
        }

        public object[] ConvertBack(object values, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
